package com.hns.can.common.aop.orgTree;


import java.util.ArrayList;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hns.can.base.entity.TmpOrganStorageEntity;
import com.hns.can.common.service.LeftTreeService;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.SearchConditionVo;
import com.hns.tool.pub.UtilString;

/**
 * 左侧树选择多项放临时表关联
 * @author wuchaoxing
 *
 */
@Aspect
@Component
public class LeftOrgRelAop {

	private final Logger logger = LoggerFactory.getLogger(LeftOrgRelAop.class); 
	
	@Autowired
	public LeftTreeService leftTreeService;

	public LeftOrgRelAop() {
		logger.debug("LeftOrgRelAop start");
	}
	
	/**
	 * @author：wucx
	 * @description：插入机构ID至临时表面向AOP
	 * @param point
	 * @param leftOrgAnnotation
	 * @date：2014年7月6日 上午11:18:17
	 */
	@SuppressWarnings("unchecked")
	@Before("execution(* *.*(..)) && @annotation(leftOrgAnnotation)")
	public void insertLeftTreeTab(JoinPoint point, LeftOrgAnnotation leftOrgAnnotation) {
		Object[] object = point.getArgs();
		if(object.length < 1)
			throw new BusinessException(MsgType.ERROR.getFlag(), "参数个数不对！");
		
		Object objectIds = object[0];
		if(!(objectIds instanceof List<?> || objectIds instanceof SearchConditionVo))
			throw new BusinessException(MsgType.ERROR.getFlag(), "参数List不是机构ID！");
		
		if(PubUtil.isEmpty(objectIds))
			return;
		
		List<String> orgIds = null;
		if(object[0] instanceof SearchConditionVo){
			orgIds = ((SearchConditionVo)object[0]).getOrgIds();
		}
		else{
			orgIds = ((List<String>)object[0]);
		}
		if(PubUtil.isEmpty(orgIds))
			return;
		
		//将选中的左侧机构ID存入表TmpOrganStorage中。
		List<TmpOrganStorageEntity> organStorageEntities = new ArrayList<TmpOrganStorageEntity>();
		for (String orgId : orgIds) {
			TmpOrganStorageEntity organStorageEntity = new TmpOrganStorageEntity();
			organStorageEntity.setId(UtilString.getUUID());
			organStorageEntity.setOrganId(orgId);
			organStorageEntities.add(organStorageEntity);
		}
		leftTreeService.savaTmpOrganStorageList(organStorageEntities);
	}
	
	/**
	 * @author：wucx
	 * @description：删除TmpOrganStorage记录
	 * @param point
	 * @param leftOrgAnnotation
	 * @date：2014年7月4日 下午5:35:37
	 */
	@After("execution(* *.*(..)) && @annotation(leftOrgAnnotation)")
	public void deleteLeftTreeTab(JoinPoint point, LeftOrgAnnotation leftOrgAnnotation) {
//		//获取当前用户
//		HttpServletRequest request = getRequest();
//		String sessionId = request.getSession().getId();
//		deleteTmpOrganStorageBySessionId(sessionId);
	}
	
    /**
     * 获取http请求
     * @return
     */
//    private HttpServletRequest getRequest() {
//    	return ((ServletRequestAttributes) RequestContextHolder                 
//    			.currentRequestAttributes()).getRequest();
//    }
    
//    /**
//     * @author：wucx
//     * @description：根据SESSIONID删除表TmpOrganStorage
//     * @param sessionId
//     * @date：2014年7月4日 下午5:27:39
//     */
//    private void deleteTmpOrganStorageBySessionId(String sessionId){
//    	List<TmpOrganStorageEntity> organStorageEntities = tmpOrganStorageDao.findBySessionId(sessionId);
//    	tmpOrganStorageDao.delete(organStorageEntities);
//    	tmpOrganStorageDao.flush();
//    }
}
